package admin.forms;

import java.net.URL;
import java.util.Map;
import java.util.TreeMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import com.google.appengine.api.blobstore.BlobInfo;
import com.google.appengine.api.blobstore.BlobInfoFactory;
import com.google.appengine.api.blobstore.BlobKey;
import com.google.appengine.api.blobstore.BlobstoreService;
import com.google.appengine.api.blobstore.BlobstoreServiceFactory;

import entities.SiteConfig;

public class SiteConfigForm {
	private static BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
	
	public static TreeMap checkValidSite(HttpServletRequest request, SiteConfig config) {
		TreeMap form = new TreeMap();
		TreeMap check = new TreeMap();
		
		String siteName = (String) request.getParameter("siteName");
		String title = (String) request.getParameter("title");
		String metaKeywords = (String) request.getParameter("metaKeywords");
		String metaDescription = (String) request.getParameter("metaDescription");
		String contactEmail = (String) request.getParameter("contactEmail");
		String googleAnalyticCode = (String) request.getParameter("googleAnalyticCode");
		String httpCharacterSet = (String) request.getParameter("httpCharacterSet");
		String submitStatus = (String) request.getParameter("submitStatus");
		String cxGoogleSearch = (String) request.getParameter("cxGoogleSearch");
		String language = (String) request.getParameter("language");
		
		if (siteName == null || siteName.isEmpty()) {
			check.put("siteName", "Site name is not empty!");
			config.setSiteName(siteName);
		} else {
			config.setSiteName(siteName);
		}
		
		if (title == null || title.isEmpty()) {
			check.put("title", "Title is not empty!");
			config.setTitle(title);
		} else {
			config.setTitle(title);
		}
		
		if (metaKeywords == null || metaKeywords.isEmpty()) {
			check.put("metaKeywords", "META Keywords is not empty!");
			config.setMetaKeywords(metaKeywords);
		} else {
			config.setMetaKeywords(metaKeywords);
		}
		
		if (metaDescription == null || metaDescription.isEmpty()) {
			check.put("metaDescription", "META Description is not empty!");
			config.setMetaDescription(metaDescription);
		} else {
			config.setMetaDescription(metaDescription);
		}
		
		if (contactEmail == null || contactEmail.isEmpty()) {
			check.put("contactEmail", "Contact email is not empty!");
			config.setContactEmail(contactEmail);
		} else {
			Pattern p = Pattern.compile("^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$");
		    Matcher m = p.matcher(contactEmail);
		    if (!m.matches()) {
		    	check.put("contactEmail", "Email is not valid");
		    }
			config.setContactEmail(contactEmail);
		}
		
		if (googleAnalyticCode != null) {
			config.setGoogleAnalyticCode(googleAnalyticCode);
		}
		
		if (submitStatus != null) {
			config.setSubmitStatus(Integer.parseInt(submitStatus));
		}
		
		if (httpCharacterSet == null || httpCharacterSet.isEmpty()) {
			check.put("httpCharacterSet", "Http Character Set code is not empty!");
			config.setHttpCharacterSet(httpCharacterSet);
		} else {
			config.setHttpCharacterSet(httpCharacterSet);
		}
		
		if (cxGoogleSearch != null) {
			config.setCxGoogleSearch(cxGoogleSearch);
		}
		
		if (language != null) {
			config.setLanguage(language);
		}
		
		form.put("checkValid", check);
		form.put("config", config);
		
		return form;
	}
	
	
	public static TreeMap checkValidLayout(HttpServletRequest request, SiteConfig config) {
		TreeMap form = new TreeMap();
		TreeMap check = new TreeMap();
		
		String layoutSiteUrl = (String) request.getParameter("layoutSiteUrl");
		String layoutCopyright = (String) request.getParameter("layoutCopyright");
		
		if (layoutSiteUrl == null || layoutSiteUrl.isEmpty()) {
			check.put("layoutSiteUrl", "Site Url is not empty!");
			config.setLayoutSiteUrl(layoutSiteUrl);
		} else {
			try {
				URL sUrl = new URL(layoutSiteUrl);
			} catch (Exception e) {
				check.put("layoutSiteUrl", "Site Url not url valid!");
			}
			config.setLayoutSiteUrl(layoutSiteUrl);
		}
		
		if (layoutCopyright == null || layoutCopyright.isEmpty()) {
			check.put("layoutCopyright", "Copyright Text is not empty!");
			config.setLayoutCopyright(layoutCopyright);
		} else {
			config.setLayoutCopyright(layoutCopyright);
		}
		
		/*//Check logo
		Map<String, BlobKey> blobs = blobstoreService.getUploadedBlobs(request);
	    BlobKey blobKey = blobs.get("logo");
	    if (blobKey != null && check.size() == 0) {
	    	BlobInfo info = new BlobInfoFactory().loadBlobInfo(blobKey);
	    	//Check is image
	    	if (!checkImage(info.getFilename())) {
	    		blobstoreService.delete(blobKey);
	    		check.put("logo", "File is not a image.");
	    	} else {
	    		//Delete old
		    	if (config.getLogo()!= null && !config.getLogo().isEmpty()) {
		    		BlobKey blobKeyOld = new BlobKey(config.getLogo());
		    		blobstoreService.delete(blobKeyOld);
		    	}
		    	config.setLogo(blobKey.getKeyString());
	    	}
	    } else if (blobKey != null) {
	    	blobstoreService.delete(blobKey);
		}*/
	    
		form.put("checkValid", check);
		form.put("config", config);
		
		return form;
	}
	
	public static TreeMap checkValidEmail(HttpServletRequest request, SiteConfig config) {
		TreeMap form = new TreeMap();
		TreeMap check = new TreeMap();
		
		form.put("checkValid", check);
		form.put("config", config);
		
		return form;
	}
	
	private static boolean checkImage(String name) {
		boolean valid = false;
		
		Pattern pattern = Pattern.compile("([^\\s]+(\\.(?i)(jpg|png|gif|bmp))$)");
		Matcher matcher = pattern.matcher(name);
		
		return matcher.matches();
	}
}
